package com.abe.libquick.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.abe.libcore.base.screen.j.AbeJFragment;
import com.abe.libquick.api.StringSmallUtils;
import com.abe.libquick.utils.NormalUtils;
import com.abe.libquick.widget.dialog.DateTimePickerDialog2;

import java.util.Date;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.text.TextUtils.isEmpty;
import static com.abe.libquick.api.StringSmallUtils.contains;

public abstract class BaseFragment extends AbeJFragment {
    public Context context;
    private Unbinder unbinder;

    protected abstract void initFContent();

    public String getText(EditText editText) {
        return editText.getText().toString().trim();
    }

    public String getText(TextView textView) {
        return textView.getText().toString().trim();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.context = getActivity();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initContent() {
        unbinder = ButterKnife.bind(this, rootView);
        initFContent();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null)
            unbinder.unbind();
    }

    @Override
    public Boolean isAutoZoom() {
        return false;
    }

    public void localEmptyMsg(int what) {
        getHandler().sendEmptyMessage(what);
    }

    public Date stringToDate(String rule, String dateStr) {
        return NormalUtils.stringToDate(rule, dateStr);
    }

    public String stringToDateStr(String rule, String dateStr) {
        return StringSmallUtils.DateToSpecialString(stringToDate(rule, dateStr));
    }

    public String dateToString(Date date, String rule) {
        return NormalUtils.dateToString(rule, date);
    }

    public String dateToString(String rule, Date date) {
        return NormalUtils.dateToString(rule, date);
    }

    public String dateStrToString(String rule, String date) {
        return dateToString(rule, SpecialStringToDate(date));
    }

    /**
     * 指定的String类型转换为时间类型格式
     */
    public Date SpecialStringToDate(String str) {
        if (isEmpty(str))
            return null;
        if (!contains(str, "Date"))
            return null;
        str = str.replace("/Date(", "").replace("+0800)/", "").trim();
        return new Date(Long.parseLong(str));
    }

    public void toast(int strRes) {
        toast(getResources().getString(strRes));
    }

    public void toast(String rule, Object... objects) {
        toast(String.format(Locale.CHINA, rule, objects));
    }

    /**
     * 日期相关操作
     */
    protected void bindDateDialog(final TextView view) {
        view.setOnClickListener(v -> showDateAndTimeDialog(view, DateTimePickerDialog2.TYPE_DATE));
    }

    //时间相关操作
    protected void bindTimeDialog(final TextView view) {
        view.setOnClickListener(v -> showDateAndTimeDialog(view, DateTimePickerDialog2.TYPE_TIME));
    }

    //日期与时间
    protected void bindDateAndTimeDialog(final TextView view) {
        view.setOnClickListener(v -> showDateAndTimeDialog(view, DateTimePickerDialog2.TYPE_DATE_TIME));
    }

    /**
     * 选择日期
     *
     * @param view 绑定标签
     * @param type 类型
     *             type:[DateTimePickerDialog2.TYPE_DATE_TIME,DateTimePickerDialog2
     *             .TYPE_TIME,DateTimePickerDialog2.TYPE_DATE]
     */
    protected void showDateAndTimeDialog(TextView view, int type) {
        DateTimePickerDialog2 dateDialog = new DateTimePickerDialog2(getActivity(), type, view);
        dateDialog.show();
    }
}